package com.dalin.platform.etl.action;

import java.util.ArrayList;
import java.util.List;

import com.dalin.framework.action.StrutsTwoAction;
import com.dalin.platform.etl.condition.EtlColumnInfoCondition;
import com.dalin.platform.etl.condition.EtlDatabaseInfoCondition;
import com.dalin.platform.etl.condition.EtlTableInfoCondition;
import com.dalin.platform.etl.model.EtlColumnInfoEntity;
import com.dalin.platform.etl.model.EtlDatabaseInfoEntity;
import com.dalin.platform.etl.model.EtlTableInfoEntity;
import com.dalin.platform.etl.service.EtlService;
import com.dalin.platform.etl.vo.ManagedDatasourceTreeVo;
/*****************************************************************************
 * @content: 类信息                                                   *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-12-06 14:01:21                                            *
 * @modDate: 2010-12-06 14:01:21                                            *
 * @version: 1.00                                                           *
 ****************************************************************************/
public class ManagedDatasourceAction extends StrutsTwoAction{
	private EtlService etlService; // 
	private Long parentId;
	private String flag;
	
    public String execute() throws Exception {
        super.printInfo("", "===== this action begin");
        ManagedDatasourceTreeVo treeVo = null;
        List<ManagedDatasourceTreeVo> resList = null;
        resList = new ArrayList<ManagedDatasourceTreeVo>();
        try {
        	if ("database".equals(flag)) {
        		EtlTableInfoCondition cond = new EtlTableInfoCondition();
        		cond.setDatabaseId(parentId);
                List<EtlTableInfoEntity> dataList = etlService
                	.queryListByCond(cond.prepareParams());
                for (EtlTableInfoEntity vo : dataList) {
                	treeVo = new ManagedDatasourceTreeVo();
                	treeVo.setId(vo.getTableId()+"");
                	treeVo.setText(vo.getTableName());
                	treeVo.setFlag("table");
                	resList.add(treeVo);
                }
        	} else if ("table".equals(flag)) {
        		EtlColumnInfoCondition cond = new EtlColumnInfoCondition();
        		cond.setTableId(parentId);
                List<EtlColumnInfoEntity> dataList = etlService
                		.queryListByCond(cond.prepareParams());
                for (EtlColumnInfoEntity vo : dataList) {
                	treeVo = new ManagedDatasourceTreeVo();
                	treeVo.setId(vo.getColumnId()+"");
                	treeVo.setText(vo.getColumnName()+"("+vo.getColumnComment()+")");
                	treeVo.setLeaf(true);
                	resList.add(treeVo);
                }
        	} else {
        		EtlDatabaseInfoCondition cond = new EtlDatabaseInfoCondition();
                List<EtlDatabaseInfoEntity> dataList = etlService.queryListByCond(cond.prepareParams());
                for (EtlDatabaseInfoEntity vo : dataList) {
                	treeVo = new ManagedDatasourceTreeVo();
                	treeVo.setId(vo.getDatabaseId()+"");
                	treeVo.setText(vo.getDatabaseName() + ":" + vo.getUserName());
                	treeVo.setFlag("database");
                	resList.add(treeVo);
                }
        	}
            setResContent(resList);
        } catch(Exception e) {
        	e.printStackTrace();
        	setResContent(e);
        }
        super.printInfo("", "===== this action end");
        return null;
    }

    

    public String getFlag() {
		return flag;
	}



	public void setFlag(String flag) {
		this.flag = flag;
	}



	public EtlService getEtlService() {
		return etlService;
	}



	public void setEtlService(EtlService etlService) {
		this.etlService = etlService;
	}



	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
}
